package ywpt.core.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import ywpt.components.acegi.domain.Operation;
import ywpt.components.acegi.domain.Role;
import ywpt.components.acegi.domain.User;
import ywpt.service.system.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * BaseAction 2007-5-7 上午12:33:00
 * 
 * @author <a href="mailto:mrwyxster@gmail.com">Oliver Wen</a>
 * 
 */
public abstract class BaseAction extends ActionSupport {
	
	public UserService userService;	
	
	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService=userService;
	}

	protected String getIPAddress() {
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected String getCurrentUserName() {
/*		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Subject currentUser = SecurityUtils.getSubject();  
		String username = null;
		if (obj instanceof UserDetails) {
			username = ((UserDetails) obj).getUsername();
		} else {
			username = obj.toString();
		}*/
		System.out.println("can reach getCurrentUserName");

		String username = null;
		User user = null;

		Session session = (Session) ActionContext.getContext().getSession();
	     
		if (null != session) {
	    	  System.out.println("getCurrentUserName session is not null" );
	          user = (User)session.getAttribute("USER_SESSION");
	          if (null!=user) {
	        	  System.out.println("getCurrentUserName user is not null" );
	              username = (String)user.getUsername();
	              if (null!=username) {
	            	  
		              System.out.println("getCurrentUserName has user:"+user.getUsername());
		              return username;
	              } else
	            	  System.out.println("username is null");
	            	  
	          } else
	        	  System.out.println("getCurrentUserName user is  null" );
	     } else
	    	 System.out.println("getCurrentUserName session is  null" );

		return null;
	}

	protected String getCurrentFullName()
	{
		String fullName = "";
		if(this.getCurrentUser()!=null)
			fullName=this.getCurrentUser().getFullName();
		return fullName;
	}
	protected User getCurrentUser() {
/*		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (obj instanceof UserDetails) {
			return (User) obj;
		}*/
		
		Subject currentUser = SecurityUtils.getSubject();  
		String username = null;
		User user = null;
	     if (null != currentUser) {
	    	 
	         Session session = currentUser.getSession(false);

	         if (null != session) {

	              user = (User)session.getAttribute("currentUser");
	              System.out.println("has user:"+user.getUsername());
	              return user;
	         }

	     }
	     System.out.println("no user!!!!");
		return null;
	}
	
	protected List<Operation> getOperationByCurrentUser(User user)
	{
		List<Operation> operation=new ArrayList<Operation>();
		Iterator<Role> roles=user.getRoles().iterator();
		while(roles.hasNext())
		{
			Role role=roles.next();
			operation.addAll(role.getOperations());
			Iterator<Operation> iterator=role.getOperations().iterator();
			while(iterator.hasNext())
			{
				operation.add(iterator.next());
			}
		}
		return operation;
	}
	
	protected HashMap<String,Operation> getOperMapByUser(User user)
	{
		HashMap<String,Operation> operation=new HashMap<String,Operation>();/*
		System.out.println(userService);
		User newUser=user;
		if(userService!=null)
		{
			newUser= userService.getUserById(user.getId());		
		}*/
		User newUser= userService.getUserById(user.getId());	
		Iterator<Role> roles=newUser.getRoles().iterator();
		while(roles.hasNext())
		{
			Role role=roles.next();
			Iterator<Operation> iterator=role.getOperations().iterator();
			while(iterator.hasNext())
			{
				Operation temp=iterator.next();
				operation.put(temp.getOperationCode().toString(),temp);
			}
		}
		return operation;
	}
	protected HashMap<String,Operation> getOperMapByRoles(Set<Role> roless)
	{
		HashMap<String,Operation> operation=new HashMap<String,Operation>();
		/*
		System.out.println(userService);
		User newUser=user;
		if(userService!=null)
		{
			newUser= userService.getUserById(user.getId());		
		}*/
		Iterator<Role> roles = roless.iterator();
		while(roles.hasNext())
		{
			Role role=roles.next();
			Iterator<Operation> iterator=role.getOperations().iterator();
			while(iterator.hasNext())
			{
				Operation temp=iterator.next();
				operation.put(temp.getOperationCode().toString(),temp);
			}
		}
		return operation;
	}
}
